package com.sin.j2me.rms;

import java.io.IOException;
import java.util.Hashtable;

import javax.microedition.rms.InvalidRecordIDException;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreFullException;
import javax.microedition.rms.RecordStoreNotFoundException;
import javax.microedition.rms.RecordStoreNotOpenException;

public class RMSManager {
	Hashtable idsmap = null;
	RecordStore rs = null;
	String name = null;

	public RMSManager(String name) throws RecordStoreFullException, RecordStoreNotFoundException, RecordStoreException, IOException {
		this.name = name+"_nvs";
		rs = RecordStore.openRecordStore(this.name, true, RecordStore.AUTHMODE_ANY, true);
		idsmap = new Hashtable();
		RecordEnumeration re = rs.enumerateRecords(null, null, false);
		int len = rs.getNumRecords();
		for(int i=0; i<len; ++i){
			int id = re.previousRecordId();
			byte[] bt = rs.getRecord(id);
			RMSNVObject rd = new RMSNVObject(bt);
			idsmap.put(rd.getName(), new Integer(id));
		}
	}
	public int set(String name, byte[] value) throws IOException, RecordStoreNotOpenException, InvalidRecordIDException, RecordStoreFullException, RecordStoreException{
		int id = 0;
		RMSNVObject rd = new RMSNVObject(name, value);
		byte[] bytes = rd.toBytes();
		if(idsmap.get(name) != null){
			id = ((Integer)idsmap.get(name)).intValue();
			rs.setRecord(id, bytes, 0, bytes.length);
		}
		else{
			id = rs.addRecord(bytes, 0, bytes.length);
			idsmap.put(name, new Integer(id));
		}
		return id;
	}
	public byte[] get(String name) throws RecordStoreNotOpenException, InvalidRecordIDException, IOException, RecordStoreException{
		if(idsmap.get(name) != null){
			int id = ((Integer)idsmap.get(name)).intValue();
			RMSNVObject rd = new RMSNVObject(rs.getRecord(id));
			return rd.getValue();
		}
		else{
			return null;
		}
	}
	public int set(RMSNVObject nv) throws RecordStoreNotOpenException, InvalidRecordIDException, RecordStoreFullException, IOException, RecordStoreException {
		return this.set(nv.getName(), nv.getValue());
	}
	public int setString(String name, String str) throws RecordStoreNotOpenException, InvalidRecordIDException, RecordStoreFullException, IOException, RecordStoreException{
		byte[] value = str.getBytes();
		return set(name, value);
	}
	public String getString(String name) throws RecordStoreNotOpenException, InvalidRecordIDException, IOException, RecordStoreException {
		byte[] value = get(name);
		if(value != null){
			return new String(value);
		}
		else{
			return null;
		}
	}
	public void close() throws RecordStoreNotOpenException, RecordStoreException {
		this.rs.closeRecordStore();
		this.rs = null;
	}
	public void clear() throws RecordStoreException{
		RecordEnumeration re = rs.enumerateRecords(null, null, false);
		int len = rs.getNumRecords();
		for(int i=0; i<len; ++i){
			int id = re.previousRecordId();
			this.rs.deleteRecord(id);
		}
		this.idsmap.clear();
	}
}

